package com.yanqu.road.server.manger.player;

import com.yanqu.road.entity.enums.eDestroyRoleStatus;
import com.yanqu.road.entity.player.DestroyRoleInfo;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.player.DestroyRoleBussiness;
import com.yanqu.road.logic.bussiness.player.UserBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.agent.inter.AgentPlayer;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;

public class DestroyRoleMgr extends TempMgr {
    private static Map<Long, DestroyRoleInfo> destroyRoleInfoMap;
    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        destroyRoleInfoMap = DestroyRoleBussiness.getDestroyRoleInfoMap();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for(DestroyRoleInfo data : destroyRoleInfoMap.values()){
            if(data.isInsertOption()){
                DestroyRoleBussiness.addDestroyRoleInfo(data);
            }else if(data.isUpdateOption()){
                DestroyRoleBussiness.updateDestroyRoleInfo(data);
            }
        }
        return true;
    }

    public static void timerTask(){
        for(DestroyRoleInfo data : destroyRoleInfoMap.values()){
            if(data.getStatus() == eDestroyRoleStatus.CoolTime.getValue() && System.currentTimeMillis() >= data.getDestroyTime()){
                synchronized (data){
                    if(data.getStatus() == eDestroyRoleStatus.CoolTime.getValue() && System.currentTimeMillis() >= data.getDestroyTime()){
                        //执行注销
                        UserInfo userInfo = UserMgr.getUserInfo(data.getUserId());
                        if(null != userInfo) {
                            String delOpenId = userInfo.getOpenId() + "_del";
                            if (UserBussiness.destroyRole(data.getUserId(), delOpenId)) {
                                userInfo.setOpenId(delOpenId);
                                data.setStatus(eDestroyRoleStatus.Destroy.getValue());
                                AgentPlayer.executeKillPlayer(data.getUserId());
                            }
                        }
                    }
                }
            }
        }
    }

    public static DestroyRoleInfo addDestroyRole(long userId){
        if(!destroyRoleInfoMap.containsKey(userId)){
            synchronized (destroyRoleInfoMap){
                if(!destroyRoleInfoMap.containsKey(userId)){
                    DestroyRoleInfo data = new DestroyRoleInfo();
                    data.setUserId(userId);
                    data.setInsertOption();
                    destroyRoleInfoMap.put(data.getUserId(), data);
                }
            }
        }
        DestroyRoleInfo data = destroyRoleInfoMap.get(userId);
        synchronized (data) {
            data.setStatus(eDestroyRoleStatus.CoolTime.getValue());
            data.setDestroyTime(System.currentTimeMillis() + DateHelper.DAY_MILLIONS * GameConfig.CHANNEL_LOGOUT_COOL_TIME);
        }
        return data;
    }

    public static void cancelDestroyRole(long userId){
        DestroyRoleInfo data = destroyRoleInfoMap.get(userId);
        if(null != data){
            synchronized (data){
                if(data.getStatus() != eDestroyRoleStatus.Destroy.getValue()){
                    data.setStatus(eDestroyRoleStatus.Default.getValue());
                    data.setDestroyTime(0);
                }
            }
        }
    }
}
